//
//  PainterTextKDJ.m
//  ChartRefactor
//
//  Created by CaoJun on 15/4/3.
//  Copyright (c) 2015年 CaoJun. All rights reserved.
//

#import "PainterTextKDJ.h"

using namespace std;
using namespace ytx;

@implementation PainterTextKDJ

- (NSString *) leftSideTextTransform:(double) value withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition
{
    return [NSString stringWithUTF8String:(Tools::Convert2FixString(value, 0) + "%").c_str()];
}

- (CGRect) drawLeftSideText:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition
{
    Quotation * data = indexPosition->getData();
    
    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();
    
    int startIndex = indexPosition->getStartDataIndex(), endIndex = indexPosition->getEndDataIndex();
    
    std::vector<std::pair<double, std::string> > leftSideTextList = data->getLeftSideText(startIndex, endIndex, rect, style);
    //    double baseValue = leftSideTextList[leftSideTextList.size() / 2].first;
    CGPoint point = CGPointMake(rect.getBeginX(), rect.getEndY());
    
    CGRect leftSideTextSize;
    
    for (vector<pair<double, string> >::const_iterator it = leftSideTextList.begin();
         it != leftSideTextList.end();
         ++it)
    {
        double value = (*it).first;
        
        NSString * text;
        if (isnan(value)) {
            text = @"";
        }
        else{
            text = [self leftSideTextTransform:value withViewModelIndexPosition:indexPosition];
        }
        
        point.y = rect.getPxY((*it).first);
        
        leftSideTextSize = [ctx fillText:text withX:point.x withY:point.y withColor:[NSString stringWithUTF8String:(*it).second.c_str()] withFont:[ NSString stringWithUTF8String:style.font.c_str() ] withSize:style.size withAnchor:LeftMiddle];
    }
    return leftSideTextSize;
}

@end
